//
//  LoginViewController.m
//  baodongPro
//
//  Created by feng on 16/8/16.
//  Copyright © 2016年 proper. All rights reserved.
//

#import "LoginViewController.h"
#import "UIView+FBDQuickCreateUI.h"
#import "AppDefineHeader.h"
#import "AppDelegate.h"
@interface LoginViewController ()
{
    
    UILabel*_topTipLabel;
    UITextField*_userTF;
    UITextField*_passwordTF;
    UIButton* _sonLoginbutton;
    UIButton* _findPasswordButton;
    UIButton* _registeNewButton;
    

}
@end

@implementation LoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    UIImageView*backIMGV=[[UIImageView alloc] init];
    backIMGV.userInteractionEnabled=YES;
    backIMGV.image=[UIImage imageNamed:@"loginBackgroud.jpg"];
    
    
    
    
    _topTipLabel=[[UILabel alloc] init];
    _topTipLabel.textAlignment=NSTextAlignmentCenter;
    _topTipLabel.font=[UIFont systemFontOfSize:20];
//    _topTipLabel.text=@"欢迎登录宝宝畅聊客户端";
    
    

    UIView*middleView=[[UIView alloc] init];
    middleView.backgroundColor=RGBA(218, 218, 218, 0.6);
    UILabel*userTipLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    userTipLabel.text=@"用户名:";
    UILabel*passwordTipLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    passwordTipLabel.text=@"密  码:";

    
    
    _userTF=[[UITextField alloc] init];
    _userTF.placeholder=@"请输入用户名";
    UIView*middleLine=[[UIView alloc] init];
    middleLine.backgroundColor=UIColorFromRGB(0xe5e5e5);
    _passwordTF=[[UITextField alloc] init];
    _passwordTF.secureTextEntry=YES;
    _passwordTF.placeholder=@"请输入密码";

    
    [self.view addSubview:backIMGV];
    [self.view addSubview:_topTipLabel];
    [self.view addSubview:middleView];
    [middleView  addSubview:userTipLabel];
    [middleView addSubview:passwordTipLabel];
    
    [middleView addSubview:_userTF];
    [middleView addSubview:middleLine];
    [middleView addSubview:_passwordTF];
    
    
    _sonLoginbutton=[UIButton buttonWithType:UIButtonTypeCustom];
    [_sonLoginbutton addTarget:self action:@selector(loginButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [_sonLoginbutton setTitle:@"登录" forState:UIControlStateNormal];
    [_sonLoginbutton setTitle:@"登录中" forState:UIControlStateHighlighted];
    _sonLoginbutton.backgroundColor=[UIColor orangeColor];
    [self.view addSubview:_sonLoginbutton];
    
    _findPasswordButton=[UIButton buttonWithType:UIButtonTypeCustom];
    _registeNewButton=[UIButton buttonWithType:UIButtonTypeCustom];
    
    [_findPasswordButton setTitle:@"找回密码" forState:UIControlStateNormal];
    [_registeNewButton setTitle:@"注册新用户" forState:UIControlStateNormal];
    
    [self.view addSubview:_findPasswordButton];
    [self.view addSubview:_registeNewButton];
    
    
    
    
    
    
    
    
    
    
    
    [backIMGV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    [_topTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10);
        make.width.offset(ScreenW);
        make.height.offset(40);
    }];
    
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.right.offset(-20);
        make.top.offset(150);
        make.height.offset(120);
        
    }];
    
    [userTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.offset(20);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    
    [passwordTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.offset(70);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    
    
    [_userTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userTipLabel.mas_right).offset(10);
        make.right.offset(10);
        make.top.offset(20);
        make.height.offset(30);
        
    }];
    
    [middleLine  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.top.equalTo(_userTF.mas_bottom).offset(10);
        make.height.offset(0.5);
        
    }];
    
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(userTipLabel.mas_right).offset(10);
        make.right.offset(10);
        make.top.offset(70);
        make.height.offset(30);
        
    }];
    

    [_sonLoginbutton  mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view.mas_bottom).offset(-320);
        make.right.offset(-40);
        make.left.offset(40);
        make.height.offset(40);
        
    }];
    
    [_findPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.offset(0);
        make.left.offset(0);
        make.size.mas_equalTo(CGSizeMake(100, 30));
        
        
    }];
    
    [_registeNewButton  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(0);
        make.right.offset(0);
        make.size.mas_equalTo(CGSizeMake(100, 30));
        
    }];
    
    
    
}


#pragma mark ------ private
-(void)loginButtonPressed:(UIButton*)sender
{

    [self appLoginUserName:_userTF.text password:_passwordTF.text];


    
}

/**
 *      @author 冯宝东
 *
 *      环信APP的用户登陆
 *
 *      @param user_Name     用户名
 *      @param user_Password 密码
 */
-(void)appLoginUserName:(NSString*)user_Name  password:(NSString*)user_Password
{
    [SVProgressHUD showWithStatus:@"登录中"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [self.view endEditing:YES];
    
    
    dispatch_queue_t indexQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(indexQueue, ^{
        
    EMError *error = [[EMClient sharedClient] loginWithUsername:user_Name  password:user_Password];
       dispatch_async(dispatch_get_main_queue(), ^{
           
           if (!error)
           {
               [[EMClient sharedClient].options setIsAutoLogin:YES];//自动登陆
               NSLog(@"登录成功");
               [USER_D  setObject:user_Name forKey:CurrentUserName];
               [USER_D  synchronize];
               _passwordTF.text=nil;
               AppDelegate*appDeleagte= (AppDelegate*)[UIApplication sharedApplication].delegate;
               appDeleagte.window.rootViewController=[appDeleagte fbd_tabBarVC];
               
               
           }else
           {
               NSLog(@"登录的描述信息：%@",error.errorDescription);
               SHOW_ALERT(error.errorDescription);
               
           }
           
           [SVProgressHUD dismiss];
           
       });
        
    });
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view  endEditing:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
